package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.*;
import com.javasm.service.RoomCategoryService;
import com.javasm.service.RoomService;
import com.javasm.service.impl.RoomCategoryServiceImpl;
import com.javasm.service.impl.RoomServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/playRoom/*")
public class RoomServlet extends BaseServlet{
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取参数
        String roomIdStr = req.getParameter("roomId");

        String roomCategroyLevel1Str = req.getParameter("roomCategroyLevel1");
        String roomCategroyLevel2Str = req.getParameter("roomCategroyLevel2");

        Integer roomCategoryL1 = null;
        if (roomCategroyLevel1Str != null && !"".equals(roomCategroyLevel1Str)){
            roomCategoryL1 = Integer.parseInt(roomCategroyLevel1Str);
        }
        Integer roomCategoryL2 = null;
        if (roomCategroyLevel2Str != null && !"".equals(roomCategroyLevel2Str)){
            roomCategoryL2 = Integer.parseInt(roomCategroyLevel2Str);
        }


        String roomOwnerCustomerStr = req.getParameter("roomOwnerCustomer");
        String roomOpenStatusStr = req.getParameter("roomOpenStatus");
        String roomPushStr = req.getParameter("roomPush");
        String roomSpeechStatusStr = req.getParameter("roomSpeechStatus");

        String roomName = req.getParameter("roomName");
        String roomNotice = req.getParameter("roomNotice");

        Integer roomId = null;
        if (roomIdStr != null && !"".equals(roomIdStr)){
            roomId = Integer.parseInt(roomIdStr);
        }

        Integer roomOwnerCustomer = null;
        if (roomOwnerCustomerStr != null && !"".equals(roomOwnerCustomerStr)){
            roomOwnerCustomer = Integer.parseInt(roomOwnerCustomerStr);
        }
        Integer roomOpenStatus = null;
        if (roomOpenStatusStr != null && !"".equals(roomOpenStatusStr)){
            roomOpenStatus = Integer.parseInt(roomOpenStatusStr);
        }
        Integer roomPush = null;
        if (roomPushStr != null && !"".equals(roomPushStr)){
            roomPush = Integer.parseInt(roomPushStr);
        }
        Integer roomSpeechStatus = null;
        if (roomSpeechStatusStr != null && !"".equals(roomSpeechStatusStr)){
            roomSpeechStatus = Integer.parseInt(roomSpeechStatusStr);
        }
        //封装对象
        Room inputRoom = new Room(roomId, roomName, roomOwnerCustomer, roomCategoryL1,roomCategoryL2, roomSpeechStatus, roomNotice, roomPush, roomOpenStatus);
        //调用service
        RoomService roomService = new RoomServiceImpl();
        Integer resNum = roomService.addPlyRoom(inputRoom);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();


    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roomIdStr = req.getParameter("roomId");
        Integer roomId = null;
        if (roomIdStr != null && !"".equals(roomIdStr)){
            roomId = Integer.parseInt(roomIdStr);
        }
        RoomService roomService = new RoomServiceImpl();
        Integer resNum = roomService.deletePlyRoomById(roomId);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roomIdStr = req.getParameter("roomId");
        String roomPushStr = req.getParameter("roomPush");
        String roomOwnerCustomerStr = req.getParameter("roomOwnerCustomer");
        String roomName = req.getParameter("roomName");
        String roomNotice = req.getParameter("roomNotice");
        Integer roomId = null;
        if (roomIdStr != null && !"".equals(roomIdStr)){
            roomId = Integer.parseInt(roomIdStr);
        }
        Integer roomPush = null;
        if (roomPushStr != null && !"".equals(roomPushStr)){
            roomPush = Integer.parseInt(roomPushStr);
        }
        Integer roomOwnerCustomer = null;
        if (roomOwnerCustomerStr != null && !"".equals(roomOwnerCustomerStr)){
            roomOwnerCustomer = Integer.parseInt(roomOwnerCustomerStr);
        }
        Room inputRoom = new Room(roomId, roomName, roomOwnerCustomer, null,null, null, roomNotice, roomPush, null);
        RoomService roomService = new RoomServiceImpl();
        Integer resNum = roomService.editPlyRoom(inputRoom);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void editPlyRoomStauts(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roomIdStr = req.getParameter("roomId");
        String roomOpenStatusStr = req.getParameter("roomOpenStatus");
        String roomSpeechStatusStr = req.getParameter("roomSpeechStatus");
        Integer roomId = null;
        if (roomIdStr != null && !"".equals(roomIdStr)){
            roomId = Integer.parseInt(roomIdStr);
        }
        Integer roomOpenStatus = null;
        if (roomOpenStatusStr != null && !"".equals(roomOpenStatusStr)){
            roomOpenStatus = Integer.parseInt(roomOpenStatusStr);
        }
        Integer roomSpeechStatus = null;
        if (roomSpeechStatusStr != null && !"".equals(roomSpeechStatusStr)){
            roomSpeechStatus = Integer.parseInt(roomSpeechStatusStr);
        }
        Room inputRoom = new Room(roomId, null, null, null,null, roomSpeechStatus, null, null, roomOpenStatus);
        RoomService roomService = new RoomServiceImpl();
        Integer resNum = roomService.editPlyRoomStauts(inputRoom);
        ReturnResult rrs = null;
        if(resNum>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }
    public void getRoomById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roomIdStr = req.getParameter("roomId");
        Integer roomId = null;
        if (roomIdStr != null && !"".equals(roomIdStr)){
            roomId = Integer.parseInt(roomIdStr);
        }
        RoomService roomService = new RoomServiceImpl();
        Room room = roomService.getPlyRoomById(roomId);
        ReturnResult rrs = null;
        if(room !=null){
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                    ReturnCodeEnum.QUERY_SUCCESS.getMsg(), room);
        }else {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_NODATA.getCode(),
                    ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }
    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        Integer page = 1;
        Integer pageSize = 10;
        if (pageStr != null && !"".equals(pageStr)){
            page = Integer.parseInt(pageStr);
        }
        if (pageSizeStr != null && !"".equals(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }
        String roomName = req.getParameter("roomName");
        String roomOwnerCustomerName = req.getParameter("roomOwnerCustomerName");
        String roomCategroyLevel1Str = req.getParameter("roomCategroyLevel1");
        String roomCategroyLevel2Str = req.getParameter("roomCategroyLevel2");

        Integer roomCategoryL1 = null;
        if (roomCategroyLevel1Str != null && !"".equals(roomCategroyLevel1Str)){
            roomCategoryL1 = Integer.parseInt(roomCategroyLevel1Str);
        }
        Integer roomCategoryL2 = null;
        if (roomCategroyLevel2Str != null && !"".equals(roomCategroyLevel2Str)){
            roomCategoryL2 = Integer.parseInt(roomCategroyLevel2Str);
        }
        Room inputRoom = new Room(null, roomName, null, roomCategoryL1, roomCategoryL2, null, null, null, null);
        inputRoom.setRoomOwnerCustomerName(roomOwnerCustomerName);
        RoomService roomService = new RoomServiceImpl();
        Integer total = roomService.countPlyRooms(inputRoom);
        //总页数计算公式 total%pageSize 能不能整除
        //能整除    total/pageSize   页
        //不能整除  total/pageSize+1  页
        Integer totalPage ;
        totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
        //检查当前要查的页数 是否超过了最大页数
        //如果超过了 则用最后一页查
        //修复bug时 有可能产生新bug
        //功能完成后 需要重新测试 之前测通的功能 可能会与现在的功能产生冲突 造成bug
        if(page!=1){
            if(page>totalPage){
                page = totalPage;
            }
        }
        List<Room> rooms = roomService.listPlyRooms(inputRoom, page, pageSize);
        PageInfo returnPageInfo = new PageInfo(page,pageSize,total);
        //3反馈操作结果
        ReturnResult rrs = new ReturnResult();
        if(rooms.size()>0){
            rrs.setCode(ReturnCodeEnum.QUERY_SUCCESS.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_SUCCESS.getMsg());

            rrs.setPageInfo(returnPageInfo);
            rrs.setReturnData(rooms);
        }else{
            rrs.setCode(ReturnCodeEnum.QUERY_NODATA.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();

    }

    public void listRoomCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String parentIdStr = req.getParameter("parentId");
        Integer parentId = null;
        if (parentIdStr != null && !"".equals(parentIdStr)){
            parentId = Integer.parseInt(parentIdStr);
        }
        RoomCategoryService roomCategoryService = new RoomCategoryServiceImpl();
        List<RoomCategory> playRoomCategories = roomCategoryService.listCategoryForSelect(parentId);
        ReturnResult rrs = null;
        if(playRoomCategories!=null&&playRoomCategories.size()>0){
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                    ReturnCodeEnum.QUERY_SUCCESS.getMsg(),playRoomCategories);
        }else {
            rrs = new ReturnResult(ReturnCodeEnum.QUERY_NODATA.getCode(),
                    ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }


    }
